﻿<template>
  <div class="app-container">
    <div class="filter-title">
      <el-form label-position="right" label-width="80px">
        <el-form-item :label="$t('panel.requestPerson')+' :'">
          <el-input
            v-model="listQuery.request_person"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('panel.requestPerson')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('public.mobile')+' :'">
          <el-input
            v-model="listQuery.mobile"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('public.mobile')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('public.itoc')+' :'">
          <el-input
            v-model="listQuery.itoc_name"
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('public.itoc')"
            @keyup.enter.native="handleFilter"
          ></el-input>
        </el-form-item>
        <el-button
          style="margin-left:20px"
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >{{ $t('table.search') }}</el-button>
        <el-button
          style="margin-left: 10px"
          class="filter-item"
          type="primary"
          @click="handleRefresh"
        >{{ $t('table.refresh') }}</el-button>
        <div v-show="!showSearchItem" class="showDown" @click="showSearchItem = true">
          <i class="el-icon-d-arrow-right"></i>
        </div>
        <div v-show="showSearchItem" class="showUp" @click="showSearchItem = false">
          <i class="el-icon-d-arrow-left"></i>
        </div>
      </el-form>
    </div>
    <div v-show="showSearchItem" class="filter-container">
      <el-form label-position="right" label-width="80px">
        <el-form-item :label="$t('public.status')+' :'">
          <el-select
            v-model="listQuery.status"
            clearable
            style="width: 150px"
            class="filter-item"
            :placeholder="$t('public.status')"
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(255,255,255,0.2)"
      border
      fit
      highlight-current-row
      style="width: 100%min-height:1000px"
    >
      <el-table-column fixed="left" align="center" :label="$t('table.id')" width="65">
        <template slot-scope="scope">
          <span>{{ (listQuery.page-1)*listQuery.limit+scope.$index+1 }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('panel.requestPerson')">
        <template slot-scope="scope">
          <span>{{ scope.row.request_person }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('public.mobile')">
        <template slot-scope="scope">
          <span>{{ scope.row.mobile }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('public.itoc')">
        <template slot-scope="scope">
          <span>{{ scope.row.itoc_name }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('panel.param')">
        <template slot-scope="scope">
          <span>{{ scope.row.param }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('panel.equipmentStatus')">
        <template slot-scope="scope">
          <span>{{ switchEs(scope.row.equipment_status) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('public.createTime')">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('public.status')">
        <template slot-scope="scope">
          <span :class="statusColor(scope.row.status)">{{ statusEsSecond(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        align="center"
        :label="$t('table.actions')"
        :width="operWidth"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleRead(scope.row)"
          >{{ $t('table.read') }}</el-button>
          <el-button
            v-if="permission.review"
            type="primary"
            size="mini"
            @click="handleReview(scope.row)"
          >{{ $t('table.review') }}</el-button>
          <el-button
            v-if="permission.update"
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row)"
          >{{ $t('table.edit') }}</el-button>
          <el-button
            v-if="permission.del"
            size="mini"
            type="danger"
            @click="handleModifyStatus(scope.row,'deleted')"
          >{{ $t('table.delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        background
        :current-page="listQuery.page"
        :page-sizes="[10,20,30,50]"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </div>
    <!-- 编辑 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="right"
        label-width="35%"
        style="width: 400px margin-left:120px"
        size="small"
      >
        <el-form-item :label="$t('panel.requestPerson')+' :'" prop="request_person">
          <el-input v-model="temp.request_person"></el-input>
        </el-form-item>
        <el-form-item :label="$t('public.mobile')+' :'" prop="mobile">
          <el-input v-model="temp.mobile"></el-input>
        </el-form-item>
        <el-form-item :label="$t('public.itoc')+' :'" prop="itoc_id">
          <select-page
            :id="temp.itoc_id"
            :name="temp.itoc_name"
            placeholder="请选择运营中心"
            key-field="id"
            show-field="name"
            :data="operationOptions"
            :count="operationCount"
            :page-size="pageSize"
            style="width:200px"
            @values="getOperationid"
            @getList="getOperationList"
          ></select-page>
        </el-form-item>
        <el-form-item :label="$t('panel.param')+' :'" prop="param">
          <el-input v-model="temp.param"></el-input>
        </el-form-item>
        <el-form-item :label="$t('public.createTime')+' :'">
          <span>{{ temp.create_time }}</span>
        </el-form-item>
        <el-form-item v-if="dialogStatus=='review'" :label="$t('public.status')+' :'" prop="status">
          <el-radio-group v-model="temp.status">
            <el-radio :label="0" disabled>审核中</el-radio>
            <el-radio :label="1">审核通过</el-radio>
            <el-radio :label="2">未通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="temp.status === 2" :label="$t('public.remark')+' :'" prop="check_remark">
          <el-input v-if="permission.review" v-model="temp.check_remark" type="textarea"></el-input>
          <span v-else>{{ temp.check_remark }}</span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button
          v-if="dialogStatus==='create'"
          type="primary"
          :disabled="!createDisabled"
          @click="createData"
        >{{ $t('table.confirm') }}</el-button>
        <el-button
          v-else
          type="primary"
          :disabled="!updateDisabled"
          @click="updateData()"
        >{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>

    <!-- 查看 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormRead">
      <el-form
        :model="temp"
        label-position="right"
        label-width="40%"
        style="width: 400px margin-left:120px"
      >
        <el-form-item :label="$t('panel.requestPerson')+' :'">
          <span>{{ temp.request_person }}</span>
        </el-form-item>
        <el-form-item :label="$t('public.mobile')+' :'">
          <span>{{ temp.mobile }}</span>
        </el-form-item>
        <el-form-item :label="$t('public.itoc')+' :'">
          <span>{{ temp.itoc_name }}</span>
        </el-form-item>
        <el-form-item :label="$t('panel.param')+' :'">
          <span>{{ temp.param }}</span>
        </el-form-item>
        <el-form-item :label="$t('panel.equipmentStatus')+' :'">
          <span>{{ switchEs(temp.equipment_status) }}</span>
        </el-form-item>
        <el-form-item :label="$t('public.createTime')+' :'">
          <span>{{ temp.create_time }}</span>
        </el-form-item>
        <el-form-item :label="$t('public.status')+' :'">
          <span>{{ statusEsSecond(temp.status) }}</span>
        </el-form-item>
        <el-form-item v-if="temp.status === 2" :label="$t('public.remark')+' :'">
          <span>{{ temp.check_remark }}</span>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import selectPage from "@/views/select/SelectPage"; // 分页选择控件

export default {
  name: "CentraApplication",
  components: {
    "select-page": selectPage
  },
  data() {
    const person = (rule, value, callback) => {
      // return false
      if (value.length > 0) {
        if (value.length > 16) {
          callback(new Error("申请人长度不能大于16个字符"));
        } else {
          var reg = /^(?!(\d+)$)(?!(\_+)$)(?!(\、+)$)[\u4e00-\u9fff\w\、]+$/; /* 定义验证表达式 */
          if (reg.test(value)) {
            /* 进行验证 */
            callback();
          } else {
            callback(
              new Error(
                "申请人只能包含汉字、英文、“_”、“（”、“）”和数字且不为纯数字或纯符号"
              )
            );
          }
        }
      } else {
        callback(new Error("请填写申请人"));
      }
    };
    const mobile = (rule, value, callback) => {
      // return false
      if (value.length > 0) {
        var reg = /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/; /* 定义验证表达式 */
        if (reg.test(value)) {
          /* 进行验证 */
          callback();
        } else {
          callback(new Error("请输入正确的联系电话"));
        }
      } else {
        callback(new Error("请填写联系电话"));
      }
    };
    const remark = (rule, value, callback) => {
      if (value) {
        if (value.length <= 0) {
          callback(new Error("请输入审核意见"));
        } else if (value.length > 300) {
          callback(new Error("审核意见不能超过300个字符"));
        } else {
          callback();
        }
      } else {
        callback(new Error("请输入审核意见"));
      }
    };
    const param = (rule, value, callback) => {
      if (value) {
        if (value.length > 30) {
          callback(new Error("硬件参数不能超过30个字符"));
        } else {
          callback();
        }
      } else {
        callback(new Error("请输入硬件参数"));
      }
    };
    return {
      showSearchItem: false,
      tableKey: 0,
      list: null,
      total: null,
      regionType: null,
      regionId: null,
      listLoading: true,
      statusOptions: [
        {
          value: 0,
          label: "审核中"
        },
        {
          value: 2,
          label: "未通过"
        }
      ],
      listQuery: {
        page: 1,
        limit: 20,
        request_person: "",
        mobile: "",
        itoc_name: "",
        status: ""
      },
      temp: {
        id: undefined,
        request_person: "",
        mobile: "",
        itoc_id: "",
        itoc_name: "",
        param: "",
        status: "",
        check_remark: ""
      },
      dialogFormVisible: false,
      dialogFormRead: false,
      dialogStatus: "",
      textMap: {
        edit: "编辑",
        create: "添加",
        review: "审核",
        read: "查看"
      },
      rules: {
        request_person: [
          { required: true, trigger: "blur", validator: person }
        ],
        mobile: [{ required: true, trigger: "blur", validator: mobile }],
        itoc_id: [
          { required: true, message: "请选择运营中心", trigger: "blur" }
        ],
        param: [{ required: true, trigger: "blur", validator: param }],
        status: [{ required: true, message: "请审核", trigger: "blur" }],
        check_remark: [{ required: true, trigger: "blur", validator: remark }]
      },
      createDisabled: true,
      updateDisabled: true,
      loading: false,
      operationOptions: [],
      operationCount: undefined,
      pageSize: 20,
      permission: {
        review: false,
        update: false,
        del: false
      }
    };
  },
  computed: {
    operWidth: function() {
      let itemCount = 1;
      if (this.permission.review) {
        itemCount += 1;
      }
      if (this.permission.update) {
        itemCount += 1;
      }
      if (this.permission.del) {
        itemCount += 1;
      }
      return itemCount * 80 + "px";
    }
  },
  created() {
    if (this.$store.state.permission.jurisdiction === "*") {
      this.permission.del = true;
      this.permission.update = true;
      this.permission.review = true;
    } else {
      const jurisdiction = this.$store.state.permission.jurisdiction.split(",");
      jurisdiction.forEach(element => {
        const id = parseInt(element);
        if (id === 169) {
          this.permission.review = true;
        }
        if (id === 170) {
          this.permission.update = true;
        }
        if (id === 171) {
          this.permission.del = true;
        }
      });
    }
    this.regionId = this.$store.state.user.id;
    this.regionType = this.$store.state.user.regionType;
    this.getList();
  },
  methods: {
    getOperationList(pageNumber = 1, search = "") {
      this.getOperation(pageNumber, search);
    },
    getOperationid(data) {
      if (data.length > 0) {
        this.temp.itoc_id = data[0].id;
        this.temp.itoc_name = data[0].name;
      } else {
        this.temp.itoc_id = undefined;
        this.temp.itoc_name = "";
      }
    },
    getList() {
      this.listLoading = true;
      const data = {
        rows: this.listQuery.limit,
        page: this.listQuery.page
      };
      if (this.listQuery.request_person) {
        data["request_person"] = this.listQuery.request_person;
      }
      if (this.listQuery.mobile) {
        data["mobile"] = this.listQuery.mobile;
      }
      if (this.listQuery.itoc_name) {
        data["itoc_name"] = this.listQuery.itoc_name;
      }
      if (this.listQuery.status || this.listQuery.status === 0) {
        data["status"] = this.listQuery.status;
      }
      const centraGetRequestPath = {
        // 获取中央控制屏列表
        company: "STQ",
        api: "pc_Centra_getRequestList",
        version: "1.0",
        data: data
      };
      this.socketApi.sendSock(JSON.stringify(centraGetRequestPath), res => {
        if (res) {
          if (res !== '{"type":"ping"}') {
            const resj = JSON.parse(res);
            if (resj.code === -1) {
              this.$message.error(resj.message);
            } else {
              this.total = resj.count;
              this.list = resj.data;
            }
          }
          this.listLoading = false;
        } else {
          this.$message.error(" 获取广告主列表失败 ");
          this.listLoading = false;
        }
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleModifyStatus(row) {
      this.$confirm("此操作将永久删除该中央控制屏, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          const centraDeletePath = {
            company: "STQ",
            api: "pc_Centra_deleteCentra",
            version: "1.0",
            data: {
              id: row.id
            }
          };
          this.socketApi.sendSock(JSON.stringify(centraDeletePath), res => {
            if (res) {
              if (res !== '{"type":"ping"}') {
                const resj = JSON.parse(res);
                if (resj.code === -1) {
                  this.$message.error(resj.message);
                } else {
                  this.getList();
                  this.$message({
                    type: "success",
                    message: "删除成功!"
                  });
                }
              }
            } else {
              this.$message.error(" 删除失败 ");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        request_person: "",
        mobile: "",
        itoc_id: "",
        itoc_name: "",
        param: "",
        status: "",
        check_remark: ""
      };
    },
    handleRefresh() {
      this.listQuery = {
        page: 1,
        limit: 20,
        request_person: "",
        mobile: "",
        itoc_name: "",
        status: ""
      };
      this.getList();
    },
    handleReview(row) {
      // 打开审核弹窗
      this.temp = Object.assign({}, row); // copy obj
      this.dialogStatus = "review";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    handleUpdate(row) {
      // 打开编辑弹窗
      this.temp = Object.assign({}, row); // copy obj
      this.dialogStatus = "edit";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    handleRead(row) {
      // 打开查看弹窗
      this.temp = Object.assign({}, row); // copy obj
      this.dialogStatus = "read";
      this.dialogFormRead = true;
    },
    updateData() {
      // 编辑中央控制屏
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          if (this.updateDisabled) {
            this.updateDisabled = false;
            const data = {
              id: this.temp.id,
              request_person: this.temp.request_person,
              mobile: this.temp.mobile,
              itoc_id: this.temp.itoc_id,
              itoc_name: this.temp.itoc_name,
              param: this.temp.param
            };
            if (this.temp.status) {
              data["status"] = this.temp.status;
            }
            if (this.temp.check_remark) {
              data["check_remark"] = this.temp.check_remark;
            }
            const centraCheckCentraPath = {
              company: "STQ",
              api: "pc_Centra_editCentra",
              version: "1.0",
              data: data
            };
            this.socketApi.sendSock(
              JSON.stringify(centraCheckCentraPath),
              res => {
                if (res) {
                  if (res !== '{"type":"ping"}') {
                    const resj = JSON.parse(res);
                    if (resj.code === -1) {
                      this.$message.error(resj.message);
                    } else {
                      this.dialogFormVisible = false;
                      this.getList();
                      this.$notify({
                        title: "成功",
                        message: "更新成功",
                        type: "success",
                        duration: 2000
                      });
                    }
                  }
                  this.listLoading = false;
                } else {
                  this.$message.error(" 更新失败 ");
                }
                setTimeout(() => {
                  this.updateDisabled = true;
                }, 1000);
              }
            );
          }
        }
      });
    },
    // 商家选择
    getMerchant(query) {
      const merchantGetPath = {
        company: "STQ",
        api: "pc_merchant_getName",
        version: "1.0",
        data: {
          name: query
        }
      };
      this.socketApi.sendSock(JSON.stringify(merchantGetPath), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    // 运营中心选择
    getOperation(pageNumber, search) {
      const industryIndexPath = {
        company: "STQ",
        api: "pc_itoc_getList",
        version: "1.0",
        data: {
          name: search,
          page: pageNumber,
          rows: this.pageSize
        }
      };
      this.socketApi.sendSock(JSON.stringify(industryIndexPath), res => {
        if (res) {
          this.socketData(res);
        }
      });
    },
    socketData(res) {
      if (res !== '{"type":"ping"}') {
        const resj = JSON.parse(res);
        if (resj.code === -1) {
          this.$message.error(resj.message);
        } else {
          if (resj.api === "pc_itoc_getList") {
            this.operationOptions = resj.data;
            this.operationCount = resj.count;
          }
        }
      }
    }
  }
};
</script>
